package com.example.administrator.printer.module.yilianyun;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.administrator.printer.base.BaseActivity;
import com.example.administrator.printer.R;
import com.example.cloudprinter.entity.yilianyun.Token;
import com.example.cloudprinter.printer.yilianyun.YilianYunPrinter;
import com.example.cloudprinter.printer.yilianyun.callback.OnAddCallback;
import com.example.cloudprinter.printer.yilianyun.callback.OnDeleteCallback;
import com.example.cloudprinter.printer.yilianyun.callback.OnPrintCallback;
import com.example.cloudprinter.printer.yilianyun.callback.OnSetVoiceCallback;
import com.example.cloudprinter.printer.yilianyun.callback.OnTokenCallback;
import com.example.cloudprinter.printer.yilianyun.format.YilianYunFormat;

import com.example.administrator.printer.utils.SP;

/**
 * 易联云打印机
 */
public class YilianYunActivity extends BaseActivity implements View.OnClickListener {
    EditText et_number, et_key, et_volume, et_voice, et_voice_content, et_content;
    Button btn_refresh_token, btn_auth, btn_delete, btn_print, btn_set_voice;

    YilianYunPrinter printer;

    SP mSP;

    final static String ACCESS_TOKEN = "access_token";
    final static String REFRESH_TOKEN = "refresh_token";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yilian_yun);

        et_number = findViewById(R.id.et_number);
        et_key = findViewById(R.id.et_key);
        et_content = findViewById(R.id.et_content);
        et_volume = findViewById(R.id.et_volume);
        et_voice = findViewById(R.id.et_voice);
        et_voice_content = findViewById(R.id.et_voice_content);
        btn_refresh_token = findViewById(R.id.btn_refresh_token);
        btn_auth = findViewById(R.id.btn_auth);
        btn_delete = findViewById(R.id.btn_delete);
        btn_print = findViewById(R.id.btn_print);
        btn_set_voice = findViewById(R.id.btn_set_voice);
        btn_refresh_token.setOnClickListener(this);
        btn_auth.setOnClickListener(this);
        btn_delete.setOnClickListener(this);
        btn_print.setOnClickListener(this);
        btn_set_voice.setOnClickListener(this);

        printer = new YilianYunPrinter();
        mSP = new SP(this);

        printer.getToken(new OnTokenCallback() {
            @Override
            public void onToken(Token token) {
                mSP.putString(ACCESS_TOKEN, token.getAccess_token());
                mSP.putString(REFRESH_TOKEN, token.getRefresh_token());
                //Toast.makeText(getApplication(), "初始化成功", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(int errNo, String errMsg) {
                //Toast.makeText(getApplication(), "初始化，原因：" + errMsg, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_refresh_token://刷新token
                printer.refreshToken(mSP.getString(REFRESH_TOKEN), new OnTokenCallback() {
                    @Override
                    public void onToken(Token token) {
                        mSP.putString(ACCESS_TOKEN, token.getAccess_token());
                        mSP.putString(REFRESH_TOKEN, token.getRefresh_token());
                        Toast.makeText(getApplication(), "初始化成功", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onFailure(int errNo, String errMsg) {
                        Toast.makeText(getApplication(), "初始化失败，原因：" + errMsg, Toast.LENGTH_SHORT).show();
                    }
                });
                break;
            case R.id.btn_auth://添加打印机
                printer.addPrinter(et_number.getText().toString(), et_key.getText().toString(), mSP.getString(ACCESS_TOKEN)
                        , new OnAddCallback() {
                            @Override
                            public void onAddSuccess() {
                                Toast.makeText(getApplication(), "添加打印机成功！", Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onAddFailure(int errNo, String errMsg) {
                                Toast.makeText(getApplication(), "添加打印机失败，原因：" + errMsg, Toast.LENGTH_SHORT).show();
                            }
                        });
                break;
            case R.id.btn_delete://删除打印机
                printer.deletePriter(et_number.getText().toString(), mSP.getString(ACCESS_TOKEN), new OnDeleteCallback() {
                    @Override
                    public void onDeleteSuccess() {
                        Toast.makeText(getApplication(), "删除打印机成功！", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onDeleteFailure(int errNo, String errMsg) {
                        Toast.makeText(getApplication(), "删除打印机失败，原因：" + errMsg, Toast.LENGTH_SHORT).show();
                    }
                });
                break;
            case R.id.btn_set_voice://设置语音
//                printer.setSound(et_number.getText().toString(), mSP.getString(ACCESS_TOKEN), "horn", et_volume.getText().toString(),
//                        new OnSetVoiceCallback() {
//                            @Override
//                            public void onSetSuccess() {
//                                Toast.makeText(getApplication(), "音量设置成功", Toast.LENGTH_SHORT).show();
//                            }
//
//                            @Override
//                            public void onSetFailure(int errNo, String errMsg) {
//                                Toast.makeText(getApplication(), "音量设置失败，原因：" + errMsg, Toast.LENGTH_SHORT).show();
//                            }
//                        });

                printer.setVoice(et_number.getText().toString(), mSP.getString(ACCESS_TOKEN), et_voice_content.getText().toString()
                        , Integer.parseInt(et_volume.getText().toString()), Integer.parseInt(et_voice.getText().toString()),
                        new OnSetVoiceCallback() {
                            @Override
                            public void onSetSuccess() {
                                Toast.makeText(getApplication(), "语音设置成功", Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onSetFailure(int errNo, String errMsg) {
                                Toast.makeText(getApplication(), "语音设置失败，原因：" + errMsg, Toast.LENGTH_SHORT).show();
                            }
                        });
                break;
            case R.id.btn_print://打印
                printer.print(et_number.getText().toString(), mSP.getString(ACCESS_TOKEN), order.getPrintTemplate(new YilianYunFormat()), "1",
                        new OnPrintCallback() {
                            @Override
                            public void onPrintSuccess() {
                                Toast.makeText(getApplication(), "开始请求打印", Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onPrintFailure(int errNo, String errMsg) {
                                Toast.makeText(getApplication(), "打印失败，原因：" + errMsg, Toast.LENGTH_SHORT).show();
                            }
                        });
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        printer.release();
    }
}
